<template>
  <div class="searchBox">
    <el-card class="box-card">
      <div class="top">
        <div class="top_left">
          <el-icon><Search /></el-icon>
          <span class="search_txt">筛选搜索 </span>
        </div>
        <div class="top_right">
          <el-button type="default">重置</el-button>
          <el-button type="primary" @click="queryUserList">查询搜索</el-button>
        </div>
      </div>
      <div class="under">
        <span>账号/姓名: </span>
        <el-input
          size="small"
          v-model="queryParams.keyword"
          placeholder="输入名字账号"
        />
      </div>
    </el-card>
  </div>
  <div class="add">
    <el-card>
      <div>
        <el-icon><Tickets /></el-icon>数据列表
      </div>
      <el-button type="primary" @click="showDialog = true">添加</el-button>
    </el-card>
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="账号" prop="username" />
    <el-table-column label="姓名" prop="nickName" />
    <el-table-column label="邮箱" prop="email" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        <span>{{ dateFormat(row.createTime) }}</span></template
      >
    </el-table-column>
    <el-table-column label="最后登录" prop="loginTime"
      ><template #default="{ row }">
        <span>{{ dateFormat(row.loginTime) }}</span></template
      ></el-table-column
    >
    <el-table-column label="是否启用" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="allocRoleHandle(row.id)"
          v-permission="'allocRole'"
          >分配角色</el-button
        >
        <el-button
          text
          type="primary"
          @click="editUser(row.id)"
          v-permission="'editUser'"
          >编辑</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryUserList"
        @current-change="queryUserList"
      />
    </div>
  </ElConfigProvider>
  <editUserVue
    :show="showDialog"
    @close="closeDialog"
    :id="editId"
  ></editUserVue>
  <allocRoleVue
    :show="showAllocRole"
    :id="allocId"
    @hide="closeAlloc"
  ></allocRoleVue>
</template>
<script setup lang="ts">
import { reactive, ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import { userListApi, switchUserStatusApi } from "./api/api";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElMessage } from "element-plus";
import { dateFormat } from "@/utils/index";
import editUserVue from "./editUser.vue";
import allocRoleVue from "./allocRole.vue";
const userList = ref<List[]>([]);

// 控制弹出
const showDialog = ref(false);
const showAllocRole = ref(false);
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) queryUserList();
};
const queryParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});
// 编辑用户
const editId = ref(0);
const editUser = (id: number) => {
  showDialog.value = true;
  editId.value = id;
};
// 全部数量
const total = ref(0);
// 用户状态
const switchStatus = (row: List) => {
  switchUserStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};
// 分配用户角色
const allocId = ref(0);
const allocRoleHandle = (id: number) => {
  showAllocRole.value = true;
  allocId.value = id;
};
const closeAlloc = (isReload?: boolean) => {
  showAllocRole.value = false;
  allocId.value = 0;
};
// 封装
const queryUserList = () => {
  userListApi(queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      queryParams.keyword = "";
      total.value = res.data.total;
    }
  });
};
queryUserList();
</script>
<style lang="less" scoped>
.searchBox {
  :deep(.el-card) {
    padding: 20px;
  }
  .top {
    position: relative;
    display: flex;
    .top_left {
      .search_txt {
        padding-left: 10px;
        font-size: 14px;
      }
    }
    .top_right {
      position: absolute;
      right: 20px;
    }
  }
  .under {
    padding: 20px 40px 0;
    font-size: 14px;
    :deep(.el-input) {
      height: 32px;
      line-height: 32px;
      width: 210px;
    }
  }
}
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
}

:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
